// components/albumCom/albumCom.js
var timeOutFn = {}
var typeNum = 0
var index = 0
var imgs = []
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    gifHeight: String,
    albumData:Object,
    imgs: {
      type: Array,
      observer(newVal, oldVal, changedPath) {
        if (newVal.length != 0){
          clearTimeout(timeOutFn)
          index = 0
          typeNum = 0
          imgs = newVal
          this.run()
        }
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    url:'',
    imgClass:'',
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 改变图片
    changeImg(){
      index = index <= imgs.length - 1 ? index : 0
      this.setData({ url: imgs[index] })
      index++
    },
    // 随机出动作
    run() {
      clearTimeout(timeOutFn)
      const num = parseInt(Math.random() * 100)/10
      let type = 0
      if (num <= 2.5) {
        type = 1
      } else if (2.5 < num && num <= 5) {
        type = 2
      } else if (5.5 < num && num <= 7.5) {
        type = 3
      } else {
        type = 4
      }
      if (type == typeNum) {
        this.run()
        return
      }
      if (typeNum == 0 ) {
        this.changeImg()
      }
      if (num <= 2.5) {
        this.move1()
      } else if (2.5 < num && num <= 5) {
        this.move2()
      } else if (5.5 < num && num <= 7.5) {
        this.move3()
      } else {
        this.move4()
      }
      typeNum = type
    },
    move1() {
      this.setData({ imgClass: 'move1' })
      let that = this
      timeOutFn = setTimeout(function () {
        that.run()
        // 调用图片更改的函数
        that.changeImg()
      }, 15000)
    },
    move2() {
      this.setData({ imgClass: 'move2' })
      let that = this
      timeOutFn = setTimeout(function () {
        that.run()
        // 调用图片更改的函数
        that.changeImg()
      }, 15000)
    },
    move3() {
      this.setData({ imgClass: 'move3' })
      let that = this
      timeOutFn = setTimeout(function () {
        that.run()
        // 调用图片更改的函数
        that.changeImg()
      }, 15000)
    },
    move4() {
      this.setData({ imgClass: 'move4' })
      let that = this
      timeOutFn = setTimeout(function () {
        that.run()
        // 调用图片更改的函数
        that.changeImg()
      }, 15000)
    },
  }
})
